﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;

namespace OpenLS.Spreadsheet
{
    internal struct IntInterval
    {
        public readonly int First;
        public readonly int Last;

        public IntInterval(int first, int last)
        {
            Contract.Requires(first <= last);
            First = first;
            Last = last;
        }
        public override string ToString()
        {
            return string.Format("[{0},{1}]", First, Last);
        }
        public bool Equals (IntInterval interval)
        {
            return interval.First == First && interval.Last == Last;
        }
        public IEnumerable<int> GetValues()
        {
            for (int i = First; i <= Last; i++)
                yield return i;
        }

        public bool Contains(int key)
        {
            return key >= First && key <= Last;
        }

        public bool Intersects(IntInterval value)
        {
            if (value.Last < this.First)
                return false;
            if (value.First > this.Last)
                return false;
            return true;
        }
    }
}